package myJs.admin


import myJs.myPkg.jquery.$

import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scala.scalajs.js.Dynamic.{global => g}
import myJs.Implicits._
import org.scalajs.dom.Event

/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("ChangePassword")
object ChangePassword {

  @JSExport("init")
  def init = {
    bootStrapValidator
  }

  def bootStrapValidator = {
    val url = g.jsRoutes.controllers.AdminController.userNameCheck().url.toString
    val dict: js.Dictionary[js.Any]  = js.Dictionary(
      "feedbackIcons" -> js.Dictionary(
        "valid" -> "glyphicon glyphicon-ok",
        "invalid" -> "",
        "validating" -> "glyphicon glyphicon-refresh",
      ),
      "fields" -> js.Dictionary(
        "password" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "请输入旧密码！"
            ),
          )
        ),
        "newPassword" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "新密码不能为空！"
            ),
            "regexp" -> js.Dictionary(
              "message" -> "新密码只能由数字和字母组成，必须同时包含大小写，且不小于6位！",
              "regexp" -> js.RegExp("^(?=[0-9a-zA-Z]*\\d)(?=[0-9a-zA-Z]*[A-Z])(?=[0-9a-zA-Z]*[a-z])[0-9a-zA-Z]{6,}$"),
            ),
            "blank" -> js.Dictionary()
          )
        ),
        "newPasswordAgain" -> js.Dictionary(
          "validators" -> js.Dictionary(
            "notEmpty" -> js.Dictionary(
              "message" -> "确认新密码不能为空！"
            ),
            "blank" -> js.Dictionary()
          )
        ),
      )
    )
    $("#form").bootstrapValidator(dict).on("success.form.bv", (e: Event) => {
      e.preventDefault()
      val formJq = $("#form")
      val bv = formJq.bv
      val newPasswordAgain = formJq.findInputByName("newPasswordAgain").myVal
      val newPassword = formJq.findInputByName("newPassword").myVal
      if (newPasswordAgain == newPassword) {
        bv.defaultSubmit()
      } else {
        bv.updateMessage("newPasswordAgain", "blank", "请输入与新密码相同字符串!")
        bv.updateStatus("newPasswordAgain", "INVALID", "blank")
      }
    })
  }

}
